﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Animation;

namespace WPFLoaders
{
    [ValueConversion(typeof(Storyboard), typeof(Storyboard))]
    public class DelayAnimationConverter : IValueConverter
    {
        private readonly static DelayAnimationConverter StaticInstance = new DelayAnimationConverter();

        public static DelayAnimationConverter Instance { get { return StaticInstance; } }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var storyboard = value as Storyboard;
            if(storyboard == null)
                throw new ArgumentException("storyboard");

            var delay = (double)System.Convert.ChangeType(parameter, typeof (double));

            storyboard = storyboard.Clone();
            storyboard.BeginTime = TimeSpan.FromSeconds(delay / storyboard.SpeedRatio);

            return storyboard;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
